package com.galaxy.service.service.system.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.galaxy.service.entity.system.Permission;
import com.galaxy.service.entity.system.RolePermission;
import com.galaxy.service.mapper.system.RolePermissionMapper;
import com.galaxy.service.service.system.PermissionService;
import com.galaxy.service.service.system.RolePermissionService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 角色权限接口实现里
 * @author caijinbang
 * @date 2018/10/27 19:14
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper,RolePermission>
    implements RolePermissionService {

  @Autowired
  private PermissionService permissionService;

  @Override
  public List<Permission> findAllResourceByRoleId(String rid) {
    List<RolePermission> rolePermissions = this.selectList(new EntityWrapper<RolePermission>().eq("role_id",rid));
    if(rolePermissions!=null){
      List<String> permissionIds = new ArrayList<>();
      rolePermissions.forEach(rolePermission -> {
        permissionIds.add(rolePermission.getPermissionId());
      });
      if(permissionIds.size()==0){
        return null;
      }
      return permissionService.selectList(new EntityWrapper<Permission>().in("id", permissionIds).orderBy("sort",true)
      );
    }
    return null;
  }
}
